<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs">
<head>
<title>无限定的名字查找</title>
<meta charset="UTF-8">
<meta name="generator" content="MediaWiki 1.21.2">
<link rel="shortcut icon" href="../../../common/favicon.ico">
<link rel="stylesheet" href="../../../common/ext.css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" href="../../../common/site_modules.css">
<style>a:lang(ar),a:lang(ckb),a:lang(fa),a:lang(kk-arab),a:lang(mzn),a:lang(ps),a:lang(ur){text-decoration:none}#toc{display:none}.editsection{display:none}
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-css:7:15cea3ec788a65b5187d4018eed543bf */</style>

<script src="../../../common/startup_scripts.js"></script>
<script>if(window.mw){
mw.config.set({"wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"cpp/language/unqualified_lookup","wgTitle":"cpp/language/unqualified lookup","wgCurRevisionId":65197,"wgArticleId":7810,"wgIsArticle":true,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Todo with reason","Todo without reason"],"wgBreakFrames":false,"wgPageContentLanguage":"zh","wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""],"wgDefaultDateFormat":"zh","wgMonthNames":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgMonthNamesShort":["","1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"],"wgRelevantPageName":"cpp/language/unqualified_lookup","wgUserVariant":"zh","wgRestrictionEdit":[],"wgRestrictionMove":[]});
}</script><script>if(window.mw){
mw.loader.implement("user.options",function(){mw.user.options.set({"ccmeonemails":0,"cols":80,"date":"default","diffonly":0,"disablemail":0,"disablesuggest":0,"editfont":"default","editondblclick":0,"editsection":0,"editsectiononrightclick":0,"enotifminoredits":0,"enotifrevealaddr":0,"enotifusertalkpages":1,"enotifwatchlistpages":0,"extendwatchlist":0,"externaldiff":0,"externaleditor":0,"fancysig":0,"forceeditsummary":0,"gender":"unknown","hideminor":0,"hidepatrolled":0,"imagesize":2,"justify":0,"math":1,"minordefault":0,"newpageshidepatrolled":0,"nocache":0,"noconvertlink":0,"norollbackdiff":0,"numberheadings":0,"previewonfirst":0,"previewontop":1,"quickbar":5,"rcdays":7,"rclimit":50,"rememberpassword":0,"rows":25,"searchlimit":20,"showhiddencats":0,"showjumplinks":1,"shownumberswatching":1,"showtoc":0,"showtoolbar":1,"skin":"cppreference2","stubthreshold":0,"thumbsize":2,"underline":2,"uselivepreview":0,"usenewrc":0,"watchcreations":0,"watchdefault":0,"watchdeletion":0,
"watchlistdays":3,"watchlisthideanons":0,"watchlisthidebots":0,"watchlisthideliu":0,"watchlisthideminor":0,"watchlisthideown":0,"watchlisthidepatrolled":0,"watchmoves":0,"wllimit":250,"variant":"zh","language":"zh","searchNs0":true,"searchNs1":false,"searchNs2":false,"searchNs3":false,"searchNs4":false,"searchNs5":false,"searchNs6":false,"searchNs7":false,"searchNs8":false,"searchNs9":false,"searchNs10":false,"searchNs11":false,"searchNs12":false,"searchNs13":false,"searchNs14":false,"searchNs15":false,"gadget-MathJax":1,"gadget-ColiruCompiler":1});;},{},{});mw.loader.implement("user.tokens",function(){mw.user.tokens.set({"editToken":"+\\","patrolToken":false,"watchToken":false});;},{},{});
/* cache key: mwiki1-mwiki_zh_:resourceloader:filter:minify-js:7:258d7cd6aa9aa67dee25e01fb6a9e505 */
}</script>
<script>if(window.mw){
mw.loader.load(["mediawiki.page.startup","mediawiki.legacy.wikibits","mediawiki.legacy.ajax"]);
}</script>
<style type="text/css">/*<![CDATA[*/
.source-cpp {line-height: normal;}
.source-cpp li, .source-cpp pre {
	line-height: normal; border: 0px none white;
}
/**
 * GeSHi Dynamically Generated Stylesheet
 * --------------------------------------
 * Dynamically generated stylesheet for cpp
 * CSS class: source-cpp, CSS id: 
 * GeSHi (C) 2004 - 2007 Nigel McNie, 2007 - 2008 Benny Baumann
 * (http://qbnz.com/highlighter/ and http://geshi.org/)
 * --------------------------------------
 */
.cpp.source-cpp .de1, .cpp.source-cpp .de2 {font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;}
.cpp.source-cpp  {font-family:monospace;}
.cpp.source-cpp .imp {font-weight: bold; color: red;}
.cpp.source-cpp li, .cpp.source-cpp .li1 {font-weight: normal; vertical-align:top;}
.cpp.source-cpp .ln {width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;}
.cpp.source-cpp .li2 {font-weight: bold; vertical-align:top;}
.cpp.source-cpp .kw1 {color: #0000dd;}
.cpp.source-cpp .kw2 {color: #0000ff;}
.cpp.source-cpp .kw3 {color: #0000dd;}
.cpp.source-cpp .kw4 {color: #0000ff;}
.cpp.source-cpp .co1 {color: #909090;}
.cpp.source-cpp .co2 {color: #339900;}
.cpp.source-cpp .coMULTI {color: #ff0000; font-style: italic;}
.cpp.source-cpp .es0 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es1 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es2 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es3 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es4 {color: #008000; font-weight: bold;}
.cpp.source-cpp .es5 {color: #008000; font-weight: bold;}
.cpp.source-cpp .br0 {color: #008000;}
.cpp.source-cpp .sy0 {color: #008000;}
.cpp.source-cpp .sy1 {color: #000080;}
.cpp.source-cpp .sy2 {color: #000040;}
.cpp.source-cpp .sy3 {color: #000040;}
.cpp.source-cpp .sy4 {color: #008080;}
.cpp.source-cpp .st0 {color: #008000;}
.cpp.source-cpp .nu0 {color: #000080;}
.cpp.source-cpp .nu6 {color: #000080;}
.cpp.source-cpp .nu8 {color: #000080;}
.cpp.source-cpp .nu12 {color: #000080;}
.cpp.source-cpp .nu16 {color:#000080;}
.cpp.source-cpp .nu17 {color:#000080;}
.cpp.source-cpp .nu18 {color:#000080;}
.cpp.source-cpp .nu19 {color:#000080;}
.cpp.source-cpp .ln-xtra, .cpp.source-cpp li.ln-xtra, .cpp.source-cpp div.ln-xtra {background-color: #ffc;}
.cpp.source-cpp span.xtra { display:block; }

/*]]>*/
</style><!--[if lt IE 7]><style type="text/css">body{behavior:url("/mwiki/skins/cppreference2/csshover.min.htc")}</style><![endif]--></head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_language_unqualified_lookup skin-cppreference2 action-view cpp-navbar">
        <!-- header -->
        <!-- /header -->
        <!-- content -->
<div id="cpp-content-base">
            <div id="content">
                <a id="top"></a>
                <div id="mw-js-message" style="display:none;"></div>
                                <!-- firstHeading -->
<h1 id="firstHeading" class="firstHeading">无限定的名字查找</h1>
                <!-- /firstHeading -->
                <!-- bodyContent -->
                <div id="bodyContent">
                                        <!-- tagline -->
                    <div id="siteSub">来自cppreference.com</div>
                    <!-- /tagline -->
                                        <!-- subtitle -->
                    <div id="contentSub"><span class="subpages">&lt; <a href="../../cpp.html" title="cpp">cpp</a>‎ | <a href="../language.html" title="cpp/language">language</a></span></div>
                    <!-- /subtitle -->
                                                            <!-- bodycontent -->
                    <div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../../cpp.html" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
<tr class="t-nv"><td colspan="5"> <a href="../language.html" title="cpp/language">语言</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../header.html" title="cpp/header">标准库头文件</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../freestanding.html" title="cpp/freestanding"> 自立与有宿主实现</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../named_req.html" title="cpp/named req">具名要求</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html#.E8.AF.AD.E8.A8.80.E6.94.AF.E6.8C.81" title="cpp/utility">语言支持库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../concepts.html" title="cpp/concepts">概念库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../error.html" title="cpp/error">诊断库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../utility.html" title="cpp/utility">工具库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../string.html" title="cpp/string">字符串库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="https://zh.cppreference.com/w/cpp/container" title="cpp/container">容器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../iterator.html" title="cpp/iterator">迭代器库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../ranges.html" title="cpp/ranges"> 范围库</a> <span class="t-mark-rev t-since-cxx20">(C++20)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../algorithm.html" title="cpp/algorithm">算法库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../numeric.html" title="cpp/numeric">数值库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../io.html" title="cpp/io">输入/输出库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../locale.html" title="cpp/locale">本地化库</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../regex.html" title="cpp/regex">正则表达式库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../atomic.html" title="cpp/atomic">原子操作库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../thread.html" title="cpp/thread">线程支持库</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../filesystem.html" title="cpp/filesystem">文件系统库</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../experimental.html" title="cpp/experimental">技术规范</a> </td></tr>
</table></div><div></div></div></div></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="../language.html" title="cpp/language">C++ 语言</a></div><div class="t-navbar-sep"> </div><div class="t-navbar-head"><a href="basic_concepts.html" title="cpp/language/basic concepts">基本概念</a><div class="t-navbar-menu"><div><div style="display:inline-block">
<div><table class="t-nv-begin" cellpadding="0" style="">
<tr class="t-nv"><td colspan="5"> <a href="../keywords.html" title="cpp/keyword"> 语言关键词</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="translation_phases.html" title="cpp/language/translation phases"> 翻译阶段</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="../comments.html" title="cpp/comment"> 注释</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="main_function.html" title="cpp/language/main function"> <code>main()</code> 函数</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="name.html" title="cpp/language/identifiers"> 名字和标识符</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="incomplete_type.html" title="cpp/language/type"> 类型</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="types.html" title="cpp/language/types"> 基础类型</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="object.html" title="cpp/language/object"> 对象</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="scope.html" title="cpp/language/scope"> 作用域</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lifetime.html" title="cpp/language/lifetime"> 对象生存期</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="storage_duration.html" title="cpp/language/storage duration"> 存储期与连接</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="definition.html" title="cpp/language/definition"> 定义和单一定义规则（ODR）</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="lookup.html" title="cpp/language/lookup"> 名字查找</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="qualified_lookup.html" title="cpp/language/qualified lookup"> 有限定名字查找</a> </td></tr>
<tr class="t-nv"><td colspan="5"> <strong class="selflink"> 无限定名字查找</strong> </td></tr>
<tr class="t-nv"><td colspan="5"> <a href="as_if.html" title="cpp/language/as if"> “如同”规则</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="ub.html" title="cpp/language/ub"> 未定义行为</a></td></tr>
<tr class="t-nv"><td colspan="5"> <a href="memory_model.html" title="cpp/language/memory model"> 内存模型</a> </td></tr>
</table></div>
</div><div></div></div></div></div><div class="t-navbar-sep"> </div></div>
<p><i>无限定</i>名，即并非出现在作用域解析运算符 <code><b>::</b></code> 右边的名字，其名字查找按下文所述检查各个<a href="scope.html" title="cpp/language/scope">作用域</a>，直到找到任何种类的至少一个声明时就停止查找，即不再继续检查别的作用域。（注意：在某些语境中所进行的查找会忽略掉一些声明，例如，对用在 <code><b>::</b></code> 左边的名字的查找将忽略函数、变量和枚举项的声明，而对用作基类说明符的名字的查找则会忽略所有的非类型的声明。）
</p><p>为了进行无限定的名字查找，来自 <a href="namespace.html" title="cpp/language/namespace">using 指令</a>所指名的命名空间中的所有声明，都被当成如同处于同时直接或间接包含这条 using 指令和所指名的命名空间的最内层的外围命名空间之中。
</p><p>对函数调用运算符左边所使用的名字（等价地也包括表达式中的运算符）所进行的无限定的名字查找，在<a href="adl.html" title="cpp/language/adl">实参依赖查找</a>中说明。
</p>
<h3><span class="mw-headline" id=".E6.96.87.E4.BB.B6.E4.BD.9C.E7.94.A8.E5.9F.9F">文件作用域</span></h3>
<p>对于在全局（顶层命名空间）作用域中，在任何函数、类或用户声明的命名空间之外所使用的名字，检查全局作用域中这次名字的使用之前的部分：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span>     <span class="co1">// n 的声明</span>
<span class="kw4">int</span> x <span class="sy1">=</span> n <span class="sy2">+</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// OK：找到了 ::n</span>
 
<span class="kw4">int</span> z <span class="sy1">=</span> y <span class="sy2">-</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// 错误：查找失败</span>
<span class="kw4">int</span> y <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>     <span class="co1">// y 的声明</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4.E4.BD.9C.E7.94.A8.E5.9F.9F">命名空间作用域</span></h3>
<p>对于在用户声明的命名空间中，在任何函数或类之外所使用的名字，在这个命名空间中这次名字的使用之前的部分中查找，然后再在包含这个命名空间的命名空间中这个命名空间的声明之前的部分中查找，直到到达全局命名空间。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// 声明</span>
<span class="kw1">namespace</span> N <span class="br0">{</span>
  <span class="kw4">int</span> m <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span>
  <span class="kw1">namespace</span> Y <span class="br0">{</span>
    <span class="kw4">int</span> x <span class="sy1">=</span> n<span class="sy4">;</span> <span class="co1">// OK，查找 ::n</span>
    <span class="kw4">int</span> y <span class="sy1">=</span> m<span class="sy4">;</span> <span class="co1">// OK，查找 ::N::m</span>
    <span class="kw4">int</span> z <span class="sy1">=</span> k<span class="sy4">;</span> <span class="co1">// 错误：查找失败</span>
  <span class="br0">}</span>
  <span class="kw4">int</span> k <span class="sy1">=</span> <span class="nu0">3</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.9C.A8.E5.91.BD.E5.90.8D.E7.A9.BA.E9.97.B4.E5.A4.96.E8.BF.9B.E8.A1.8C.E5.AE.9A.E4.B9.89">在命名空间外进行定义</span></h3>
<p>对于命名空间成员变量，在其命名空间之外的定义中所使用的名字，要以在命名空间之内使用的名字相同的方式进行查找：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> X <span class="br0">{</span>
    <span class="kw4">extern</span> <span class="kw4">int</span> x<span class="sy4">;</span> <span class="co1">// 声明，不是定义</span>
    <span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// 找到第一个</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span> <span class="co1">// 找到第二个</span>
<span class="kw4">int</span> X<span class="sy4">::</span><span class="me2">x</span> <span class="sy1">=</span> n<span class="sy4">;</span> <span class="co1">// 找到了 X::n，设置 X::x 为 1</span></pre></div></div>
<h3><span class="mw-headline" id=".E9.9D.9E.E6.88.90.E5.91.98.E5.87.BD.E6.95.B0.E7.9A.84.E5.AE.9A.E4.B9.89">非成员函数的定义</span></h3>
<p>对于在函数中，无论是其函数体还是作为默认实参的一部分而使用的名字，当这个函数是某个用户声明的或者全局的命名空间的成员时，要在包含这次名字使用的块之中，这次使用之前的部分中查找，然后查找其外围块中这个块开始前的部分，等等，直到到达函数体的块为止。然后再在声明了这个函数的命名空间中，直到使用了这个名字的函数定义（并不是其声明）之前的部分中进行查找，然后查找其外围命名空间，等等。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> A <span class="br0">{</span>
   <span class="kw1">namespace</span> N <span class="br0">{</span>
       <span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
       <span class="kw4">int</span> i<span class="sy1">=</span><span class="nu0">3</span><span class="sy4">;</span> <span class="co1">// 找到第三个（如果第二个没找到）</span>
    <span class="br0">}</span>
    <span class="kw4">int</span> i<span class="sy1">=</span><span class="nu0">4</span><span class="sy4">;</span> <span class="co1">// 找到第四个（如果第三个没找到）</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> i<span class="sy1">=</span><span class="nu0">5</span><span class="sy4">;</span> <span class="co1">// 找到第五个（如果第四个没找到）</span>
 
<span class="kw4">void</span> A<span class="sy4">::</span><span class="me2">N</span><span class="sy4">::</span><span class="me2">f</span><span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    <span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span> <span class="co1">// 找到第二个（如果第一个没找到）</span>
    <span class="kw1">while</span><span class="br0">(</span><span class="kw2">true</span><span class="br0">)</span> <span class="br0">{</span>
       <span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// 找到第一个：查找结束</span>
       <a href="../io/cout.html"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> i<span class="sy4">;</span>
    <span class="br0">}</span>
<span class="br0">}</span>
 
<span class="co1">// int i; // 找不到这个</span>
 
<span class="kw1">namespace</span> A <span class="br0">{</span>
  <span class="kw1">namespace</span> N <span class="br0">{</span>
    <span class="co1">// int i; // 找不到这个</span>
  <span class="br0">}</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E7.B1.BB.E7.9A.84.E5.AE.9A.E4.B9.89">类的定义</span></h3>
<p>对于在<a href="class.html" title="cpp/language/class">类的定义</a>中所使用的名字（包括基类说明符和嵌套类定义），当出现于成员函数体、成员函数的默认实参、成员函数的异常规定、默认成员初始化器（其中该成员可能属于定义在外围类体内的嵌套类）以外的任何位置时，要在下列作用域中查找：
</p>
<div class="t-li1"><span class="t-li">a)</span> 类体之中直到这次使用点之前的部分</div>
<div class="t-li1"><span class="t-li">b)</span> （各个）基类的整个类体，找不到声明时，递归到基类的基类中</div>
<div class="t-li1"><span class="t-li">c)</span> 当这个类是<a href="nested_classes.html" title="cpp/language/nested types">嵌套类</a>时，其外围类体中直到这个类的定义之前的部分以及外围类的（各个）基类的整个类体</div>
<div class="t-li1"><span class="t-li">d)</span> 当这个类是<a href="class.html#.E5.B1.80.E9.83.A8.E7.B1.BB" title="cpp/language/class">局部类</a>或局部类的嵌套类时，定义了这个类的块作用域中直到其定义点之前的部分</div>
<div class="t-li1"><span class="t-li">e)</span> 当这个类是命名空间的成员，或者命名空间成员类的嵌套类，或者命名空间成员函数的局部类时，查找这个命名空间作用域中直到这个类、其外围类或函数的定义之前的部分。若所查找的是由<a href="friend.html" title="cpp/language/friend">友元</a>声明所引入的名字：这种情况下仅考虑其最内层的外围命名空间，否则的话，对外围命名空间的查找将照常持续直到全局作用域。
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">namespace</span> M <span class="br0">{</span>
    <span class="co1">// const int i = 1; // 找不到这个</span>
    <span class="kw1">class</span> B <span class="br0">{</span>
        <span class="co1">// static const int i = 3; // 找到了第三个（但之后会被访问检查所拒绝）</span>
    <span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span>
<span class="co1">// const int i = 5; // 找到了第五个</span>
<span class="kw1">namespace</span> N <span class="br0">{</span>
    <span class="co1">// const int i = 4; // 找到了第四个</span>
    <span class="kw1">class</span> Y <span class="sy4">:</span> <span class="kw1">public</span> M<span class="sy4">::</span><span class="me2">B</span> <span class="br0">{</span>
        <span class="co1">// static const int i = 2; // 找到了第二个</span>
        <span class="kw1">class</span> X <span class="br0">{</span>
            <span class="co1">// static const int i = 1; // 找到了第一个</span>
            <span class="kw4">int</span> a<span class="br0">[</span>i<span class="br0">]</span><span class="sy4">;</span> <span class="co1">// i 的使用</span>
            <span class="co1">// static const int i = 1; // 找不到这个</span>
        <span class="br0">}</span><span class="sy4">;</span>
        <span class="co1">// static const int i = 2; // 找不到这个</span>
    <span class="br0">}</span><span class="sy4">;</span>
    <span class="co1">// const int i = 4; // 找不到这个</span>
<span class="br0">}</span>
<span class="co1">// const int i = 5; // 找不到这个</span></pre></div></div></div>
<h3><span class="mw-headline" id=".E6.B3.A8.E5.85.A5.E7.B1.BB.E5.90.8D">注入类名</span></h3>
<p>对于在类或类模板或其派生类或类模板中所使用的这个类或类模板的名字，无限定名字查找将会找到当前进行定义的类，如同其名字是由成员声明（以公开成员访问）的方式所引入的一样。更多细节见<a href="injected-class-name.html" title="cpp/language/injected-class-name">注入类名</a>。
</p>
<h3><span class="mw-headline" id=".E6.88.90.E5.91.98.E5.87.BD.E6.95.B0.E7.9A.84.E5.AE.9A.E4.B9.89">成员函数的定义</span></h3>
<p>对于在成员函数体、成员函数的默认实参、成员函数的异常说明或默认成员初始化器中所使用的名字，进行查找的各个作用域和<a href="unqualified_lookup.html#.E7.B1.BB.E7.9A.84.E5.AE.9A.E4.B9.89">类的定义</a>中的相同，但要考虑这个类的整个作用域，而不仅是使用了这个名字的声明之前的部分。对于嵌套类来说，其外围类的整个类体都要进行查找。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> B <span class="br0">{</span>
    <span class="co1">// int i; // 找到第三个</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">namespace</span> M <span class="br0">{</span>
    <span class="co1">// int i; // 找到第五个</span>
    <span class="kw1">namespace</span> N <span class="br0">{</span>
        <span class="co1">// int i; // 找到第四个</span>
        <span class="kw1">class</span> X <span class="sy4">:</span> <span class="kw1">public</span> B <span class="br0">{</span>
            <span class="co1">// int i; // 找到第二个</span>
            <span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
            <span class="co1">// int i; // 也找到第二个</span>
        <span class="br0">}</span><span class="sy4">;</span>
        <span class="co1">// int i; // 找到第四个</span>
    <span class="br0">}</span>
<span class="br0">}</span>
<span class="co1">// int i; // 找到第六个</span>
<span class="kw4">void</span> M<span class="sy4">::</span><span class="me2">N</span><span class="sy4">::</span><span class="me2">X</span><span class="sy4">::</span><span class="me2">f</span><span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
    <span class="co1">// int i; // 找到第一个</span>
    i <span class="sy1">=</span> <span class="nu0">16</span><span class="sy4">;</span>
    <span class="co1">// int i; // 找不到这个</span>
<span class="br0">}</span>
<span class="kw1">namespace</span> M <span class="br0">{</span>
  <span class="kw1">namespace</span> N <span class="br0">{</span>
    <span class="co1">// int i; // 找不到这个</span>
  <span class="br0">}</span>
<span class="br0">}</span></pre></div></div>
<dl><dd> 无论哪种方式，当检查派生类的基类时，需要遵守下列规则，它们有时被称为<a href="https://en.wikipedia.org/wiki/Dominance_(C%2B%2B)" class="extiw" title="enwiki:Dominance (C++)">虚继承中的优先性</a>：
</dd></dl>
 <table class="t-rev-begin">
<tr class="t-rev t-until-cxx11"><td>当子对象 <code>A</code> 是子对象 <code>B</code> 的基类子对象时，子对象 <code>B</code> 中找到的成员的名字将隐藏掉子对象 <code>A</code> 中相同的成员名。（但要注意这并不会隐藏继承晶格中并非 <code>B</code> 的基类的任何其他的 <code>A</code> 的非虚副本。这条规则仅当虚继承时才有效。）由 using 声明所引入的名字，被当成是包含这个声明的类中的名字来处理。检查了各个基类之后，其结果集合必须要么包含来自同一个类型的子对象的静态成员的声明，要么包含来自同一个子对象的非静态成员的声明。</td>
<td><span class="t-mark-rev t-until-cxx11">(C++11 前)</span></td></tr>
<tr class="t-rev t-since-cxx11"><td>构造一个<i>查找集合</i>，它由一些声明和在其中找到了这些声明的子对象所构成。将 using 声明替换为它们所代表的成员，将类型的声明（包括注入类名）替换为它们所代表的类型。若 <code>C</code> 为在其作用域中使用了这个名字的类，则首先检查 <code>C</code>。若 <code>C</code> 中的声明列表为空，则对其各个直接基类 <code>Bi</code> 各自构造其查找集合（当 <code>Bi</code> 自身也有基类时，递归地应用这条规则）。构造完成后，将各直接基类的查找集合根据以下规则合并为 <code>C</code> 的查找集合：
<dl><dd><ul><li> 若 <code>Bi</code> 中的声明集合为空，则其被丢弃
</li><li> 若迄今所构建的 <code>C</code> 的查找集合仍为空，则将其替换为 <code>Bi</code> 的查找集合
</li><li> 对于 <code>Bi</code> 的查找集合中的各个子对象，若它们都是已经加入到 <code>C</code> 的查找集合中的至少一个子对象的基类子对象，则丢弃 <code>Bi</code> 的查找集合
</li><li> 若已经加入到 <code>C</code> 的查找集合中的每个子对象均为 <code>Bi</code> 的查找集合中至少一个子对象的基类子对象，则丢弃 <code>C</code> 的查找集合，将之替换为 <code>Bi</code> 的查找集合
</li><li> 否则，若 <code>Bi</code> 和 <code>C</code> 中的声明集合不同，则合并的结果有歧义：<code>C</code> 的新查找集合包含一个无效声明，和之前合并入 <code>C</code> 中的各子对象和由 <code>Bi</code> 所引入的子对象的并集。这个无效的查找集合如果在之后被丢弃了，那么它并不会导致错误。
</li><li> 否则，<code>C</code> 的新查找集合具有之前合并入 <code>C</code> 和由 <code>Bi</code> 所引入的共同的声明集合和子对象的并集
</li></ul>
</dd></dl>
</td>
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td></tr>
</table> 
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> X <span class="br0">{</span> <span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> B1<span class="sy4">:</span> <span class="kw1">virtual</span> X <span class="br0">{</span> <span class="kw4">void</span> f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> B2<span class="sy4">:</span> <span class="kw1">virtual</span> X <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> D <span class="sy4">:</span> B1, B2 <span class="br0">{</span>
    <span class="kw4">void</span> foo<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
        X<span class="sy4">::</span><span class="me2">f</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK，调用了 X::f（有限定查找）</span>
        f<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// OK，调用了 B1::f（无限定查找）</span>
<span class="co1">// C++98 规则：B1::f 隐藏 X::f，因此即便从 D 通过 B2 可以访问到 X::f，</span>
<span class="co1">// 它也不能从 D 中的名字查找所找到。</span>
<span class="co1">// C++11 规则：在 D 中对 f 的查找集合并未找到任何东西，继续处理其基类</span>
<span class="co1">//  在 B1 中对 f 的查找集合找到了 B1::f，并且完成查找</span>
<span class="co1">// 合并时替换了空集，此时在 C 中 对 f 的查找集合包含 B1 中的 B1::f</span>
<span class="co1">//  在 B2 中对 f 的查找集合并未找到任何东西，继续处理其基类</span>
<span class="co1">//    在 X 中对 f 的查找找到了 X::f</span>
<span class="co1">//  合并时替换了空集，此时在 B2 中对 f 的查找集合包含 X 中的 X::f</span>
<span class="co1">// 当向 C 中合并时发现在 B2 的查找集合中的每个子对象（X）都是</span>
<span class="co1">// 已经合并的各个子对象（B1）的基类，因此 B2 的集合被丢弃</span>
<span class="co1">// C 剩下来的就是在 B1 中所找到的 B1::f</span>
<span class="co1">// (如果使用 struct D : B2, B1，则最后的合并将会*替换掉*</span>
<span class="co1">//  C 此时已经合并的 X 中的 X::f，因为已经加入到 C 中的每个子对象（就是 X）</span>
<span class="co1">//  都是新集合（B1）中的至少一个子对象的基类，</span>
<span class="co1">//  其最终结果是一样的：C 的查找集合只包含在 B1 中找到的 B1::f）</span>
    <span class="br0">}</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<dl><dd> 如果无限定的名字查找找到了 <code>B</code> 的静态成员，<code>B</code> 的嵌套类型，在 <code>B</code> 中声明的枚举项，则即便在所检查的类的继承树中有多个 <code>B</code> 类型的非虚基类子对象，它也是无歧义的：
</dd></dl>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> V <span class="br0">{</span> <span class="kw4">int</span> v<span class="sy4">;</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> A <span class="br0">{</span>
        <span class="kw4">int</span> a<span class="sy4">;</span>
        <span class="kw4">static</span> <span class="kw4">int</span> s<span class="sy4">;</span>
        <span class="kw2">enum</span> <span class="br0">{</span> e <span class="br0">}</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> B <span class="sy4">:</span> A, <span class="kw1">virtual</span> V <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> C <span class="sy4">:</span> A, <span class="kw1">virtual</span> V <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">struct</span> D <span class="sy4">:</span> B, C <span class="br0">{</span> <span class="br0">}</span><span class="sy4">;</span>
 
<span class="kw4">void</span> f<span class="br0">(</span>D<span class="sy3">&amp;</span> pd<span class="br0">)</span> <span class="br0">{</span>
        <span class="sy2">++</span>pd.<span class="me1">v</span><span class="sy4">;</span> <span class="co1">// OK：只有一个 v，因为只有一个虚基类子对象</span>
        <span class="sy2">++</span>pd.<span class="me1">s</span><span class="sy4">;</span> <span class="co1">// OK：只有一个静态的 A::s，即便在 B 和 C 中都找到了它</span>
        <span class="kw4">int</span> i <span class="sy1">=</span> pd.<span class="me1">e</span><span class="sy4">;</span> <span class="co1">// OK：只有一个枚举符 A::e，即便在 B 和 C 中都找到了它</span>
        <span class="sy2">++</span>pd.<span class="me1">a</span><span class="sy4">;</span> <span class="co1">// 错误，有歧义：B 中的 A::a 和 C 中的 A::a</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.8F.8B.E5.85.83.E5.87.BD.E6.95.B0.E7.9A.84.E5.AE.9A.E4.B9.89">友元函数的定义</span></h3>
<p>对于在授予友元关系的类体之中的<a href="friend.html" title="cpp/language/friend">友元</a>函数的定义中所使用的名字，无限定的名字查找按照与成员函数相同的方式进行。对于定义于类体之外的<a href="friend.html" title="cpp/language/friend">友元</a>函数中所使用的名字，无限定的名字查找按照与命名空间中的函数相同的方式进行。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">3</span><span class="sy4">;</span> <span class="co1">// f1 找到的第三个，f2 找到的第二个</span>
<span class="kw1">struct</span> X <span class="br0">{</span>
    <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> i <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span> <span class="co1">// f1 找到的第二个，f2 找不到这个</span>
    <span class="kw1">friend</span> <span class="kw4">void</span> f1<span class="br0">(</span><span class="kw4">int</span> x<span class="br0">)</span>
    <span class="br0">{</span>
        <span class="co1">// int i; // 找到第一个</span>
        i <span class="sy1">=</span> x<span class="sy4">;</span> <span class="co1">// 找到并改动 X::i</span>
    <span class="br0">}</span>
    <span class="kw1">friend</span> <span class="kw4">int</span> f2<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="co1">// static const int i = 2; // f1 在类作用域中的任何地方找到第二个</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">void</span> f2<span class="br0">(</span><span class="kw4">int</span> x<span class="br0">)</span> <span class="br0">{</span>
    <span class="co1">// int i; // 找到第一个</span>
    i <span class="sy1">=</span> x<span class="sy4">;</span> <span class="co1">// 找到并改动 ::i</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.8F.8B.E5.85.83.E5.87.BD.E6.95.B0.E7.9A.84.E5.A3.B0.E6.98.8E">友元函数的声明</span></h3>
<p>对于在使来自其他类的成员函数为友元的<a href="friend.html" title="cpp/language/friend">友元</a>函数声明的声明符中所使用的名字，如果该名字不是任何模板实参的一部分，则无限定的查找首先检查成员函数所在类的整个作用域。如果未能在这个作用域中找到（或者如果这个名字是模板实参的一部分），则继续以如同对授予友元关系的类的成员函数进行查找的方式继续查找。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co1">// 这个类的成员函数被作为友元</span>
<span class="kw1">struct</span> A <span class="br0">{</span> 
    <span class="kw1">typedef</span> <span class="kw4">int</span> AT<span class="sy4">;</span>
    <span class="kw4">void</span> f1<span class="br0">(</span>AT<span class="br0">)</span><span class="sy4">;</span>
    <span class="kw4">void</span> f2<span class="br0">(</span><span class="kw4">float</span><span class="br0">)</span><span class="sy4">;</span>
    <span class="kw1">template</span> <span class="sy1">&lt;</span><span class="kw1">class</span> T<span class="sy1">&gt;</span> <span class="kw4">void</span> f3<span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
 
<span class="co1">// 这个类授予友元关系</span>
<span class="kw1">struct</span> B <span class="br0">{</span>
    <span class="kw1">typedef</span> <span class="kw4">char</span> AT<span class="sy4">;</span>
    <span class="kw1">typedef</span> <span class="kw4">float</span> BT<span class="sy4">;</span>
    <span class="kw1">friend</span> <span class="kw4">void</span> A<span class="sy4">::</span><span class="me2">f1</span><span class="br0">(</span>AT<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 对 AT 的查找找到的是 A::AT</span>
    <span class="kw1">friend</span> <span class="kw4">void</span> A<span class="sy4">::</span><span class="me2">f2</span><span class="br0">(</span>BT<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 对 BT 的查找找到的是 B::BT </span>
    <span class="kw1">friend</span> <span class="kw4">void</span> A<span class="sy4">::</span><span class="me2">f3</span><span class="sy1">&lt;</span>AT<span class="sy1">&gt;</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 对 AT 的查找找到的是 B::AT </span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<h3><span class="mw-headline" id=".E9.BB.98.E8.AE.A4.E5.AE.9E.E5.8F.82">默认实参</span></h3>
<p>对于在函数声明的<a href="default_arguments.html" title="cpp/language/default arguments">默认实参</a>中所使用的名字，或者在构造函数的<a href="constructor.html" title="cpp/language/initializer list">成员初始化器</a>的 <span class="t-spar">表达式</span> 部分所使用的名字，在检查其外围的块、类或命名空间作用域之前，首先会找到函数形参的名字：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">class</span> X <span class="br0">{</span>
    <span class="kw4">int</span> a, b, i, j<span class="sy4">;</span>
<span class="kw1">public</span><span class="sy4">:</span>
    <span class="kw4">const</span> <span class="kw4">int</span><span class="sy3">&amp;</span> r<span class="sy4">;</span>
    X<span class="br0">(</span><span class="kw4">int</span> i<span class="br0">)</span><span class="sy4">:</span> r<span class="br0">(</span>a<span class="br0">)</span>, <span class="co1">// 将 X::r 初始化为指代 X::a</span>
              b<span class="br0">(</span>i<span class="br0">)</span>, <span class="co1">// 将 X::b 初始化为形参 i 的值</span>
              i<span class="br0">(</span>i<span class="br0">)</span>, <span class="co1">// 将 X::i 初始化为形参 i 的值</span>
              j<span class="br0">(</span>this<span class="sy2">-</span><span class="sy1">&gt;</span>i<span class="br0">)</span> <span class="co1">// 将 X::j 初始化为 X::i 的值</span>
    <span class="br0">{</span> <span class="br0">}</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> a<span class="sy4">;</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span> a, <span class="kw4">int</span> b <span class="sy1">=</span> a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：对 a 的查找找到了形参 a，而不是 ::a</span>
                         <span class="co1">// 但在默认实参中不允许使用形参</span></pre></div></div>
<h3><span class="mw-headline" id=".E9.9D.99.E6.80.81.E6.95.B0.E6.8D.AE.E6.88.90.E5.91.98.E7.9A.84.E5.AE.9A.E4.B9.89">静态数据成员的定义</span></h3>
<p>对于在<a href="static.html" title="cpp/language/static">静态数据成员</a>的定义中所使用的名字，其查找按照与对成员函数的定义中所使用的名字相同的方式进行。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> X <span class="br0">{</span>
    <span class="kw4">static</span> <span class="kw4">int</span> x<span class="sy4">;</span>
    <span class="kw4">static</span> <span class="kw4">const</span> <span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">1</span><span class="sy4">;</span> <span class="co1">// 找到第一个</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">2</span><span class="sy4">;</span> <span class="co1">// 找到第二个</span>
<span class="kw4">int</span> X<span class="sy4">::</span><span class="me2">x</span> <span class="sy1">=</span> n<span class="sy4">;</span> <span class="co1">// 找到了 X::n，将 X::x 设置为 1 而不是 2</span></pre></div></div>
<h3><span class="mw-headline" id=".E6.9E.9A.E4.B8.BE.E9.A1.B9.E7.9A.84.E5.A3.B0.E6.98.8E">枚举项的声明</span></h3>
<p>对于在<a href="enum.html" title="cpp/language/enum">枚举项的声明</a>的初始化器部分中所使用的名字，在无限定的名字查找处理其外围的块、类或命名空间作用域之前，会首先找到同一个枚举中之前所声明的枚举项。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">const</span> <span class="kw4">int</span> RED <span class="sy1">=</span> <span class="nu0">7</span><span class="sy4">;</span>
<span class="kw2">enum</span> <span class="kw1">class</span> color <span class="br0">{</span>
    RED,
    GREEN <span class="sy1">=</span> RED<span class="sy2">+</span><span class="nu0">2</span>, <span class="co1">// RED 找到了 color::RED ，而不是 ::RED ，因此 GREEN = 2</span>
    BLUE <span class="sy1">=</span> <span class="sy4">::</span><span class="me2">RED</span><span class="sy2">+</span><span class="nu0">4</span> <span class="co1">// 有限定查找找到 ::RED ， BLUE = 11</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<h3><span class="mw-headline" id=".E5.87.BD.E6.95.B0_try_.E5.9D.97.E7.9A.84_catch_.E5.AD.90.E5.8F.A5">函数 try 块的 catch 子句</span></h3>
<p>对于在<a href="function-try-block.html" title="cpp/language/function-try-block">函数 try 块</a>的 catch 子句中所使用的名字，其查找按照如同对在函数体的最外层块的最开始处使用的名字一样进行（特别是，函数形参是可见的，但这个最外层块中声明的名字则不可见）。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">3</span><span class="sy4">;</span> <span class="co1">// 找到第三个</span>
<span class="kw4">int</span> f<span class="br0">(</span><span class="kw4">int</span> n <span class="sy1">=</span> <span class="nu0">2</span><span class="br0">)</span> <span class="co1">// 找到第二个</span>
<span class="kw1">try</span> <span class="br0">{</span>
   <span class="kw4">int</span> n <span class="sy1">=</span> <span class="sy2">-</span><span class="nu0">1</span><span class="sy4">;</span>  <span class="co1">// 找不到这个</span>
<span class="br0">}</span> <span class="kw1">catch</span><span class="br0">(</span>...<span class="br0">)</span> <span class="br0">{</span>
   <span class="co1">// int n = 1; // 找到第一个</span>
   <a href="../error/assert.html"><span class="kw774">assert</span></a><span class="br0">(</span>n <span class="sy1">==</span> <span class="nu0">2</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 对 n 的查找找到了函数形参 f</span>
   <span class="kw1">throw</span><span class="sy4">;</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E9.87.8D.E8.BD.BD.E7.9A.84.E8.BF.90.E7.AE.97.E7.AC.A6">重载的运算符</span></h3>
<p>对于在表达式中所使用的<a href="expressions.html#.E8.BF.90.E7.AE.97.E7.AC.A6" title="cpp/language/expressions">运算符</a>（比如在 <code>a+b</code> 中使用的 <code>operator+</code>），其查找规则和对在如 <code>operator+(a,b)</code> 这样的显式函数调用表达式中所使用的运算符是有所不同的：当处理表达式时要分别进行两次查找：对非成员的运算符重载，也对成员运算符重载（对于同时允许两种形式的运算符）。然后按<a href="overload_resolution.html" title="cpp/language/overload resolution">重载解析</a>所述将这两个集合与内建的运算符重载以平等的方式合并到一起。而当使用显式函数调用语法时，则进行常规的无限定名字查找：
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw1">struct</span> A <span class="br0">{</span><span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">void</span> operator<span class="sy2">+</span><span class="br0">(</span>A, A<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 用户定义的非成员 operator+</span>
 
<span class="kw1">struct</span> B <span class="br0">{</span>
    <span class="kw4">void</span> operator<span class="sy2">+</span><span class="br0">(</span>B<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 用户定义的成员 operator+</span>
    <span class="kw4">void</span> f <span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
 
A a<span class="sy4">;</span>
 
<span class="kw4">void</span> B<span class="sy4">::</span><span class="me2">f</span><span class="br0">(</span><span class="br0">)</span> <span class="co1">// B 的成员函数定义</span>
<span class="br0">{</span>
    operator<span class="sy2">+</span><span class="br0">(</span>a,a<span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 错误：在成员函数中的常规名字查找</span>
                    <span class="co1">// 找到了 B 的作用域中的 operator+ 的声明</span>
                    <span class="co1">// 并于此停下，而不会达到全局作用域</span>
    a <span class="sy2">+</span> a<span class="sy4">;</span> <span class="co1">// OK：成员查找找到了 B::operator+，非成员查找</span>
           <span class="co1">// 找到了 ::operator+(A,A)，重载决议选中了 ::operator+(A,A)</span>
<span class="br0">}</span></pre></div></div>
<h3><span class="mw-headline" id=".E6.A8.A1.E6.9D.BF.E7.9A.84.E5.AE.9A.E4.B9.89">模板的定义</span></h3>
<p>对于在模板的定义中所使用的<a href="dependent_name.html" title="cpp/language/dependent name">非待决名</a>，当检查该模板的定义时将进行无限定的名字查找。在这个位置与声明之间的绑定并不会受到在实例化点可见的声明的影响。而对于在模板定义中所使用的<a href="dependent_name.html" title="cpp/language/dependent name">待决名</a>，其查找则推迟到得知其模板实参之时。此时，<a href="adl.html" title="cpp/language/adl">ADL</a> 将同时在模板的定义语境和在模板的实例化语境中检查可见的<span class="t-rev-inl t-until-cxx11"><span>具有外部连接的</span> <span><span class="t-mark-rev t-until-cxx11">(C++11 前)</span></span></span>函数声明，而非 ADL 的查找则只检查在模板的定义语境中可见的<span class="t-rev-inl t-until-cxx11"><span>具有外部连接的</span> <span><span class="t-mark-rev t-until-cxx11">(C++11 前)</span></span></span>函数声明。（换句话说，在模板定义之后添加新的函数声明，除非通过 ADL 否则仍是不可见的。）如果在 ADL 查找所检查的命名空间中，在某个别的翻译单元中声明了一个具有外部连接的更好的匹配声明，或者如果当同样检查这些翻译单元时其查找会导致歧义，则其行为是未定义的。无论哪种情况，如果某个基类取决于某个模板形参，则无限定名字查找不会检查它的作用域（在定义点和实例化点都不会）。
</p>
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">char</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// f 的第一个声明</span>
 
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> T<span class="sy1">&gt;</span> 
<span class="kw4">void</span> g<span class="br0">(</span>T t<span class="br0">)</span> <span class="br0">{</span>
    f<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy4">;</span>    <span class="co1">// 非待决名：名字查找找到了 ::f(char) 并于此时绑定</span>
    f<span class="br0">(</span>T<span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 待决名：查找推迟</span>
    f<span class="br0">(</span>t<span class="br0">)</span><span class="sy4">;</span>    <span class="co1">// 待决名：查找推迟</span>
<span class="co1">//  dd++;    // 非待决名：名字查找未找到声明</span>
<span class="br0">}</span>
 
<span class="kw2">enum</span> E <span class="br0">{</span> e <span class="br0">}</span><span class="sy4">;</span>
<span class="kw4">void</span> f<span class="br0">(</span>E<span class="br0">)</span><span class="sy4">;</span>   <span class="co1">// f 的第二个声明</span>
<span class="kw4">void</span> f<span class="br0">(</span><span class="kw4">int</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// f 的第三个声明</span>
<span class="kw4">double</span> dd<span class="sy4">;</span>
 
<span class="kw4">void</span> h<span class="br0">(</span><span class="br0">)</span> <span class="br0">{</span>
    g<span class="br0">(</span>e<span class="br0">)</span><span class="sy4">;</span>  <span class="co1">// 实例化 g&lt;E&gt;，此处</span>
           <span class="co1">// 对 'f' 的第二次和第三次使用</span>
           <span class="co1">// 进行查找并找到了 ::f(char)（常规查找）和 ::f(E)（ADL）</span>
           <span class="co1">// 然后重载解析选择了 ::f(E)。</span>
           <span class="co1">// 这调用了 f(char)，然后两次调用 f(E)</span>
    g<span class="br0">(</span><span class="nu0">32</span><span class="br0">)</span><span class="sy4">;</span> <span class="co1">// 实例化 g&lt;int&gt;，此处</span>
           <span class="co1">// 对 'f' 的第二次和第三次使用</span>
           <span class="co1">// 进行了查找仅找到了 ::f(char)</span>
           <span class="co1">// 然后重载解析选择了 ::f(char)</span>
           <span class="co1">// 这三次调用了 f(char)</span>
<span class="br0">}</span>
 
<span class="kw1">typedef</span> <span class="kw4">double</span> A<span class="sy4">;</span>
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> T<span class="sy1">&gt;</span> <span class="kw1">class</span> B <span class="br0">{</span>
   <span class="kw1">typedef</span> <span class="kw4">int</span> A<span class="sy4">;</span>
<span class="br0">}</span><span class="sy4">;</span>
<span class="kw1">template</span><span class="sy1">&lt;</span><span class="kw1">class</span> T<span class="sy1">&gt;</span> <span class="kw1">struct</span> X <span class="sy4">:</span> B<span class="sy1">&lt;</span>T<span class="sy1">&gt;</span> <span class="br0">{</span>
   A a<span class="sy4">;</span> <span class="co1">// 对 A 的查找找到了 ::A (double)，而不是 B&lt;T&gt;::A</span>
<span class="br0">}</span><span class="sy4">;</span></pre></div></div>
<p>注：关于这条规则的相关缘由和其影响，请参见<a href="dependent_name.html#.E6.9F.A5.E6.89.BE.E8.A7.84.E5.88.99" title="cpp/language/dependent name">待决名的查找规则</a>。
</p>
<h3><span class="mw-headline" id=".E6.A8.A1.E6.9D.BF.E5.90.8D">模板名</span></h3>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成<br>原因：模板名后的 -&gt; 和 . 的双作用域查找 </td></tr></table>
<h3><span class="mw-headline" id=".E6.A8.A1.E6.9D.BF.E5.A4.96.E7.9A.84.E7.B1.BB.E6.A8.A1.E6.9D.BF.E6.88.90.E5.91.98">模板外的类模板成员</span></h3>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style=""><tr><td class="mbox-empty-cell"></td><td class="mbox-text" style="">本节未完成 </td></tr></table>
<h3><span class="mw-headline" id=".E5.BC.95.E7.94.A8">引用</span></h3>
<div class="t-ref-std-c++11">
<ul><li> C++11 standard (ISO/IEC 14882:2011): 
</li></ul>
<dl><dd><ul><li> 3.4 Name lookup [basic.lookup]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 10.2 Member name lookup [class.member.lookup]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 14.6 Name resolution [temp.res]
</li></ul>
</dd></dl>
</div>
<div class="t-ref-std-c++98">
<ul><li> C++98 standard (ISO/IEC 14882:1998): 
</li></ul>
<dl><dd><ul><li> 3.4 Name lookup [basic.lookup]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 10.2 Member name lookup [class.member.lookup]
</li></ul>
</dd></dl>
<dl><dd><ul><li> 14.6 Name resolution [temp.res]
</li></ul>
</dd></dl>
</div>
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
<ul><li> <a href="qualified_lookup.html" title="cpp/language/qualified lookup">有限定名字查找</a> 
</li><li> <a href="scope.html" title="cpp/language/scope">作用域</a> 
</li><li> <a href="adl.html" title="cpp/language/adl">实参依赖查找</a> 
</li><li> <a href="function_template.html" title="cpp/language/function template">模板实参推导</a>
</li><li> <a href="overload_resolution.html" title="cpp/language/overload resolution">重载决议</a>
</li></ul>

<!-- 
NewPP limit report
Preprocessor visited node count: 2222/1000000
Preprocessor generated node count: 7970/1000000
Post‐expand include size: 38597/2097152 bytes
Template argument size: 14934/2097152 bytes
Highest expansion depth: 17/40
Expensive parser function count: 0/100
-->

<!-- Saved in parser cache with key mwiki1-mwiki_zh_:pcache:idhash:7810-0!*!0!!zh!*!zh!* and timestamp 20200304171703 -->
</div>                    <!-- /bodycontent -->
                                        <!-- printfooter -->
                    <div class="printfooter">
                    来自“<a href="https://zh.cppreference.com/mwiki/index.php?title=cpp/language/unqualified_lookup&amp;oldid=65197">https://zh.cppreference.com/mwiki/index.php?title=cpp/language/unqualified_lookup&amp;oldid=65197</a>”                    </div>
                    <!-- /printfooter -->
                                                            <!-- catlinks -->
                    <!-- /catlinks -->
                                                            <div class="visualClear"></div>
                    <!-- debughtml -->
                                        <!-- /debughtml -->
                </div>
                <!-- /bodyContent -->
            </div>
        </div>
        <!-- /content -->
        <!-- footer -->
        <div id="cpp-footer-base" class="noprint">
            <div id="footer">
                        <div id="cpp-navigation">
            <h5>导航</h5>
            <ul><li><a href="https://zh.cppreference.com/w/cpp/language/unqualified_lookup">Online version</a></li><li>Offline version retrieved 2020-03-14 18:00.</li></ul></div>
                        <ul id="footer-info">
                                    <li id="footer-info-lastmod"> 本页面最后修改于2020年3月4日 (星期三) 09:17。</li>
                                    <li id="footer-info-viewcount">此页面已被浏览过6,668次。</li>
                            </ul>
                    </div>
        </div>
        <!-- /footer -->
        <script>if(window.mw){
mw.loader.state({"site":"loading","user":"missing","user.groups":"ready"});
}</script>
<script src="../../../common/skin_scripts.js"></script>
<script>if(window.mw){
mw.loader.load(["mediawiki.action.view.postEdit","mediawiki.user","mediawiki.page.ready","mediawiki.searchSuggest","mediawiki.hidpi","ext.gadget.MathJax","ext.gadget.ColiruCompiler"], null, true);
}</script>
<script src="../../../common/site_scripts.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2828341-1']);
_gaq.push(['_setDomainName', 'cppreference.com']);
_gaq.push(['_trackPageview']);
</script><!-- Served in 0.072 secs. -->
	</body>
<!-- Cached 20200305033927 -->
</html>